﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ECCSystem.BizRule;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;
using WindowControlLib;

namespace ECCSystem
{
    public partial class frm_Ext_Merchandise : Form
    {
        public frm_Ext_Merchandise()
        {
            InitializeComponent();
        }

        public long pAnnotationID { get; set; }

        private void frm_Ext_Merchandise_Load(object sender, EventArgs e)
        {

            this.InitComboxStatus();


            MerchandiseDao _merchandiseDao = new MerchandiseDao();
            
            //MerchandiseEntity _entity = new MerchandiseEntity();
            this.dataGridView1.DataSource =
                this.ConvertDto(_merchandiseDao.GetByAnnotationId(pAnnotationID));
        }

        private void InitComboxStatus()
        {
            //Init status combox
            DICOFFAnnotatonStatusDao _dicoffAnnotatonStatusDao = new DICOFFAnnotatonStatusDao();

            IList <DICOFFAnnotatonStatusEntity> _list =
                _dicoffAnnotatonStatusDao.Search();

            //DICOFFAnnotatonStatusEntity _entity = new DICOFFAnnotatonStatusEntity();
            //_entity.ID = -1;
            //_entity.Name = "";
            //this.comboBox_Status.Items.Add(_entity);

            foreach (var dicoffAnnotatonStatusEntity in _list)
            {
                this.comboBox_Status.Items.Add(dicoffAnnotatonStatusEntity);
            }

            this.comboBox_Status.DisplayMember = "Name";
            this.comboBox_Status.ValueMember = "ID";

            this.comboBox_Status.SelectedIndex = 0;
        }

        private IList<MerchandiseGridViewDto> ConvertDto(IList<MerchandiseEntity> _listSrc)
        {
            IList<MerchandiseGridViewDto> _list = new List<MerchandiseGridViewDto>();

            foreach (var item in _listSrc)
            {
                _list.Add(new MerchandiseGridViewDto(item));
            }
            return _list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(comboBox_Status.SelectedIndex == -1)
            {
                MessageBox.Show("请选择状态！");
                return;
            }

            OFFAnnotationDao _offAnnotationDao = new OFFAnnotationDao();
            OFFAnnotationEntity _entity = new OFFAnnotationEntity();
            _entity.ID = this.pAnnotationID;
            _entity =
            _offAnnotationDao.GetById(_entity);

            _entity.dicOFFAnnotatonStatus = 
                comboBox_Status.Items[comboBox_Status.SelectedIndex] as DICOFFAnnotatonStatusEntity;

            _entity.FollowUser = ClsCommon.gStrOperatorName;
            _entity.FollowDate = DateTime.Now;

            _offAnnotationDao.SaveOrUpdate(_entity);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                string vMerchandiseNo =
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

                MerchandiseSearch _merchandiseSearch = new MerchandiseSearch();
                this.Enabled = false;
                this.dataGridView1.Enabled = false;
                this.btn_Save.Enabled = false;
                bool res = false;
                try
                {
                    res =
                        _merchandiseSearch.Search(vMerchandiseNo);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    if (ex.Message.Contains("TimeOut"))
                        MessageBox.Show("查询超时，请重新打开查询画面！");
                }


                if (!res)
                {
                    MessageBox.Show("请检查CSC工具是否打开到查询画面！");
                }

                this.dataGridView1.Enabled = true;
                this.btn_Save.Enabled = true;
                this.Enabled = true;

            }
        }
    }

    internal class MerchandiseGridViewDto
    {
        public MerchandiseGridViewDto(MerchandiseEntity _entity)
        {

            //ID = _entity.ID;
            MerchandiseNo = _entity.MerchandiseNo;
            AnnotationID = _entity.AnnotationID;
        }

        //public long ID { get; set; }

        public long AnnotationID { get; set; }
        public String MerchandiseNo { get; set; }

    }
}
